<?php

session_start();

require_once('IRequestHandler.php');
require_once('IResponseHandler.php');
require_once('ICredentials.php');
require_once('SecurityException.php');

require_once('SimpleCredentials.php');

class Authenticator {
	
	private $authAdapter;
	
	public function __construct(IAuthenticationAdapter $authAdapter) {
		$this->authAdapter = $authAdapter;
	}	
	
	private function initSession($userName, $token) {		
		$_SESSION['loggedon'] = true;
		$_SESSION['username'] = $userName;
		$_SESSION['token'] = $token;
	}	
	
	public function authenticate($userName, $password) {
		// TODO: remove implementation code and replace with factory
		$credentials = new SimpleCredentials($userName, $password);
		
		$token = $this->authAdapter->checkCredentials($credentials);
		
		// initialise user session
		$this->initSession($userName, $token);
		
		return $token;			
	}
	
	public function isValidToken($token) {
		if ($token == $_SESSION['token']) {
			return true;
		}
		
		return false;
	}
		
}